﻿using System;
using System.Collections.Generic;

using System.Text;
using BlueLotus7.MetaWeblogUtil.Contract;
using BlueLotus7.MetaWeblogUtil.Contract.Interfaces;
using CookComputing.XmlRpc;

namespace BlueLotus7.MetaWeblogUtil.Contract
{
    /// <summary>
    /// Base abstract class for blog agent
    /// </summary>
    /// <typeparam name="TProxy"></typeparam>
    public abstract class BaseAgent<TProxy> : IMetaWeblogAgent<TProxy> where TProxy : IXmlRpcProxy
    {
        protected virtual string XmlRpcUrlFormat
        {
            get { return string.Empty; }
        }

        #region Properties

        /// <summary>
        /// Login user name of account
        /// </summary>
        public string LoginName
        {
            get;
            protected set;
        }

        /// <summary>
        /// Login user password of account
        /// </summary>
        public string Password
        {
            get;
            protected set;
        }

        /// <summary>
        /// App key of blog service
        /// </summary>
        public string AppKey
        {
            get;
            protected set;
        }

        /// <summary>
        /// Identity of blog xml-RPC service
        /// </summary>
        public string XmlRpcUrlIdentity
        {
            get;
            protected set;
        }

        /// <summary>
        /// Proxy instance
        /// </summary>
        public TProxy Proxy
        {
            get;
            protected set;
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="appKey">App key of blog service</param>
        /// <param name="loginName">Login user name of account</param>
        /// <param name="password">Login password of account</param>
        /// <param name="xmlRpcUrlIdentity">Identity of blog xml-RPC service. This parameter is optional.</param>
        public BaseAgent(string appKey, string loginName, string password, string xmlRpcUrlIdentity)
            : base()
        {
            this.LoginName = loginName;
            this.Password = password;
            this.AppKey = appKey;
            this.XmlRpcUrlIdentity = xmlRpcUrlIdentity;

            Initialize();
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Initialize the data of agent
        /// </summary>
        protected virtual void Initialize()
        {
            if (this.AppKey == null)
            {
                this.AppKey = string.Empty;
            }

            if (this.XmlRpcUrlIdentity == null)
            {
                this.XmlRpcUrlIdentity = string.Empty;
            }

            this.Proxy = XmlRpcProxyGen.Create<TProxy>();

            if (!string.IsNullOrEmpty(XmlRpcUrlFormat))
            {
                this.Proxy.Url = string.Format(XmlRpcUrlFormat, this.XmlRpcUrlIdentity);
            }
        }

        #endregion
    }
}
